package com.sbxApp.controller;

import com.common.entity.Result;
import com.sbxApp.entity.CloudDisk;
import com.sbxApp.service.CloudDiskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 何剑
 * @Date: 2023/3/15
 * @Note:
 **/
@RestController
@RequestMapping("/cloudDiskCommon")
@Api(tags = "云盘")
public class CloudDiskCommonController {

    @Resource
    private CloudDiskService cloudDiskService;


    @GetMapping("fileList")
    @ApiOperation("获取文件列表")
    public Result fileList(@RequestParam String id,
                           @RequestParam(defaultValue = "false", required = false) Boolean self,
                           @RequestParam(defaultValue = "1", required = false) Integer pageIndex,
                           @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        return Result.page(cloudDiskService.fileList(id, self, pageIndex, pageSize));
    }

    @PostMapping
    @ApiOperation("上传文件")
    public Result uploadFile(@RequestPart("file") MultipartFile file,
                             @RequestParam(defaultValue = "public") String visit,
                             @RequestParam String parentId,

                             @RequestParam String name,
                             @RequestParam String bucket) {
        cloudDiskService.uploadFile(file, visit, parentId, name, bucket);
        return Result.success();
    }

    @GetMapping
    @ApiOperation("下载文件")
    public Result downFile(@RequestParam("id") String id, HttpServletResponse response) {
        cloudDiskService.downFile(id, response);
        return Result.success();
    }

    @PostMapping("dir")
    @ApiOperation("创建文件夹")
    public Result createDir(CloudDisk cloudDisk) {
        cloudDiskService.createDir(cloudDisk);
        return Result.success();
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除文件(夹)")
    public Result delFileOrDir(@PathVariable String id) {
        cloudDiskService.delFileOrDir(id);
        return Result.success();
    }

    @PutMapping("{id}")
    @ApiOperation("修改文件(夹)名称")
    public Result putFileOrDir(@PathVariable("id") String id,
                               @RequestParam(required = false) String name,
                               @RequestParam(required = false) String visit) {
        cloudDiskService.putFileOrDir(id, name, visit);
        return Result.success();
    }


    @PostMapping("{originId}/cp")
    @ApiOperation("复制文件")
    public Result cpFile() {
        cloudDiskService.cpFile();
        return Result.success();
    }

}
